var Post = require("../../../class/user/post.c");
var _Post = new Post();
module.exports = {
    prefix: "/post/",
    get: {
        "listbypage": {
            des: {
                name: "getPostListByPageInfo",
                content: "根据分页信息获取话题列表",
                params: [
                    {
                        key: "page",
                        type: "Number"
                    },
                    {
                        key: "num",
                        type: "Number"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取话题列表");
                try {
                    var data = self.request.query;
                    var _list = yield _Post.getListByPage(data);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "listbymodule/:_module": {
            des: {
                name: "getPostListByModule",
                content: "根据版块信息获取话题列表",
                params: [
                    {
                        params: "_module",
                        type: "string"
                    },
                    {
                        key: "num",
                        type: "Number"
                    },
                    {
                        key: "page",
                        type: "Number"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取版块下的话题列表");
                try {
                    var _module = self.params._module;
                    var data = self.request.query;
                    var _list = yield _Post.getListByModule(_module, data);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "count": {
            des: {
                name: "postCount",
                content: "话题统计",
                params: [
                    {
                        key: "creater_id",
                        type: "string"
                    },
                    {
                        key: "_module_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取话题统计");
                try {
                    var data = self.request.query;
                    var _count = yield _Post.count(data);
                    self.body = _count;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "detail/:_id": {
            des: {
                name: "getPostDetail",
                content: "获取话题详情",
                params: [
                    {
                        params: "_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取话题详情");
                try {
                    var _id = self.params._id;
                    var _detail = yield _Post.detail(_id);
                    self.body = _detail;

                    // 计算访问同一个话题的时间间隔，超过15分钟，访问量+1
                    self.session.post ? true : self.session.post = {};
                    // 检测post存储的数据长度，如果达到15个,则删除第一个,节约内存
                    // 这里是有可能导致访问量增加的
                    self.session.post.hasOwnProperty(_id) ? true : self.session.post = checkJsonLength(self.session.post, 15);
                    var _time = (new Date()).getTime();
                    if (_time - (self.session.post[_id] || 0) > 900000) {
                        self.session.post[_id] = (new Date()).getTime();
                        yield _Post.update({
                            _id: _id,
                            viewtotal: parseInt(_detail.viewtotal || 0) + 1
                        });
                    };
                    // console.log(self.session.post[_id]);
                } catch (e) {
                    console.log(e);
                    self.body = printError(e);
                };
            }
        }
    }
}